package com.hachimi.spiritanimal

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Surface
//import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
//import androidx.compose.ui.text.style.TextForegroundStyle.Unspecified.alpha
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.coerceAtLeast
import androidx.compose.ui.unit.dp

val imageList = listOf(
    R.drawable.image1,
    R.drawable.image2,
    R.drawable.image3,
    R.drawable.image4,
    R.drawable.image5,
    R.drawable.image6,
    R.drawable.image7,
    R.drawable.image8,
    R.drawable.image9,
    R.drawable.image10
)

@Composable
fun ImageSwitcherWithAnimation3() {
    val configuration = LocalConfiguration.current
    val screenHeightDp = with(LocalDensity.current) { configuration.screenHeightDp.dp.toPx().toDp() }
    val itemCount = imageList.size
    val topPadding = screenHeightDp * 0.08f
    val grayScaleMatrix = remember { ColorMatrix().apply { setToSaturation(0f) } }

    LazyColumn(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .padding(top = topPadding)
            .fillMaxSize()
    ) {
        itemsIndexed(imageList) { index, imageResId ->
            var isGrayScale by remember { mutableStateOf(false) }
            val painter = painterResource(id = imageResId)

            val imageHeight = remember((screenHeightDp - topPadding), itemCount) {
                if (itemCount > 0) {
                    ((screenHeightDp - topPadding) / itemCount).coerceAtLeast(40.dp)
                } else {
                    0.dp
                }
            }

            Box(
                modifier = Modifier
                    .height(imageHeight)
                    .clickable { isGrayScale = !isGrayScale }
                    .alpha(1f) // 保持 alpha 为 1f
            ) {
                Image(
                    painter = painter,
                    contentDescription = "Image $index",
                    modifier = Modifier.height(imageHeight),
                    contentScale = ContentScale.Crop,
                    colorFilter = if (isGrayScale) {
                        ColorFilter.colorMatrix(grayScaleMatrix)
                    } else {
                        null
                    }
                )
            }
        }
    }
}